package com.itgaoshu.hospitalproject.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itgaoshu.hospitalproject.bean.Lrecord;
import com.itgaoshu.hospitalproject.bean.Pay;
import com.itgaoshu.hospitalproject.bean.Register;
import com.itgaoshu.hospitalproject.service.LPayService;
import com.itgaoshu.hospitalproject.service.LadminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("liao")
public class LPayController {
    @Autowired
    private LPayService lPayService;
    @Autowired
    private LadminService ladminService;

    //跳转到缴费管理页面
    @RequestMapping("pay")
    public String pay(){
        return "liao/pay";
    }

    //查询该患者的缴费记录根据患者id来查询
    @RequestMapping("selPays")
    @ResponseBody
    public Object selPays(Integer page, Integer limit, Register register){
        PageHelper.startPage(page,limit);
        List<Pay> pays = lPayService.selPays(register);
        PageInfo pageInfo = new PageInfo(pays);
        Map<String,Object> tableDate=new HashMap<>();
        tableDate.put("code",0);
        tableDate.put("msg","");
        tableDate.put("count",pageInfo.getTotal());
        tableDate.put("data",pageInfo.getList());
        return tableDate;
    }

    //根据患者id来查询患者的信息,同时进行预交的操作时添加预交金额的操作，以及查询预交金额的记录的操作，
    //同时还要进行已用金额和余额的操作
    @RequestMapping("selPay")
    @ResponseBody
    public Object selPay(Register register,Double moneys){
        //添加预交金的操作，在患者信息表中
        //先判断是否是预交金
        if(moneys!=0){
            //在预交金中进行添加操作
            register.setMoney(moneys);
            lPayService.updatePay(register);
            //在缴费记录中进行添加预交金缴费记录操作
            lPayService.addPay(register);
        }
        List<Register> registers = ladminService.selRegister(register);
        return registers;
    }

    //查询余额，即就是查询已用了多少钱，然后还剩余多少钱
    @ResponseBody
    @RequestMapping("selSurplus")
    public Object selSurplus(Lrecord lrecord){
        List<Lrecord> lrecords = lPayService.selSurplus(lrecord);
        return lrecords;
    }

}
